﻿$(function () {
	var editButton = '<button type="button" class="btn js_button_edit">...</button>';
	var changeButton = '<button type="button" class="btn edit_button_change js_button_change hidden">Change content</button>';
	var editorDiv = '<div class="js_editor_div" />';

	function initEditors() {
		$(".js_editor").each(function () {
			$(this).append(editButton);
		});
		var counter = 0;
		$(".js_button_edit").click(function () {
			counter++;
			var valueElement = $(this).siblings().first();
			var btnEditId = 'btn_edit_' + counter;
			$(editorDiv).insertBefore(valueElement).attr('id', btnEditId);
			var editor = CKEDITOR.appendTo(btnEditId, {}, valueElement.val());
			$(this).hide();
			valueElement.hide();
			$(changeButton).insertAfter($('#' + btnEditId)).show().click(function () {
				valueElement.val(editor.getData());
			});
		});
	}
	initEditors();
});